package com.zwj.learning.timetable.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.common.service.UserService;
import com.zwj.learning.timetable.common.TokenThreadLocal;
import com.zwj.learning.timetable.constant.RedisKey;
import com.zwj.learning.timetable.dto.CourseDto;
import com.zwj.learning.timetable.entity.Course;
import com.zwj.learning.timetable.service.CourseService;
import com.zwj.learning.timetable.mapper.CourseMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 23088
 * @description 针对表【course(课程)】的数据库操作Service实现
 * @createDate 2022-11-09 13:16:27
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
        implements CourseService {

    @DubboReference
    private UserService userService;


    @Override
    public Object getCourseByStudentId(String semester, String semesterWeek) {

        String currentToken = TokenThreadLocal.getCurrentToken();
        Long currentTokenId = userService.getLoginUserId(currentToken);


        LambdaQueryWrapper<Course> courseWrapper = new LambdaQueryWrapper<>();
        courseWrapper.eq(Course::getSemesterId, Integer.valueOf(semester))
                .eq(Course::getStudentId, currentTokenId);

        List<Course> courses = this.list(courseWrapper);

        //筛选处理学期周
        Iterator<Course> iterator = courses.iterator();
        final List<CourseDto> courseDtos = new ArrayList<>();
        while (iterator.hasNext()) {

            Course next = iterator.next();
            List<String> split =
                    Arrays.asList(next.getSemesterWeekIds().split(","));

            if (!split.contains(semesterWeek))
                iterator.remove();

        }

        courses.forEach(course-> {

            int length = course.getWeekIds().split(",").length;
            for (int i = 0; i < length; i++) {
                CourseDto courseDto = new CourseDto();
                courseDto.setWeekId(course.getWeekIds().split(",")[i]);
                courseDto.setTimeId(course.getTimeIds().split(",")[i]);
                BeanUtil.copyProperties(course,courseDto);
                courseDtos.add(courseDto);
            }
        });
        return courseDtos;
    }

    @Override
    public Object getCourseCount(String semester) {
        String currentToken = TokenThreadLocal.getCurrentToken();
        Long currentTokenId = userService.getLoginUserId(currentToken);


        LambdaQueryWrapper<Course> courseWrapper = new LambdaQueryWrapper<>();
        courseWrapper.eq(Course::getSemesterId, Integer.valueOf(semester))
                .eq(Course::getStudentId, currentTokenId);

        return this.count(courseWrapper);
    }
}




